<?php

class calendar {
	function form($object, $loc) {
		global $user;

		$i18n = exponent_lang_loadFile('datatypes/calendar.php');

		if (!defined('SYS_FORMS')) require_once(BASE.'subsystems/forms.php');
		exponent_forms_initialize();

		$form = new form();
		if (!isset($object->id)) {
			$object->title = '';
			$object->body = '';
			$object->eventstart = aboid_getCurrentTime();
			$object->eventend = aboid_getCurrentTime()+3600;
			
			$object->repeat_type = -1;
			$object->approve = 0;
			
		} else {
			$form->meta('id',$object->id);
		}
		
		$have_permission=exponent_permissions_check(array("administrate", "approve", "manage_approval"), $loc);
		
		$form->register('title',$i18n['title'],new textcontrol($object->title));
		$form->register('body',$i18n['body'],new texteditorcontrol($object->body,5,90));
		
		$form->register('eventstart',$i18n['eventstart'],new yuidatetimecontrol($object->eventstart,"",true,true,false, false));
		$form->register('eventend',$i18n['eventend'],new yuidatetimecontrol($object->eventend,"",true,true,false, false));
	
		$repeat_type = array();
		$repeat_type[-1]="No repeat";
		$repeat_type[0]="Daily";
		$repeat_type[1]="Weekly";
        $repeat_type[2]="Monthly";
		$repeat_type[3]="Yearly";
		$form->register("repeat_type","Repeat type", new dropdowncontrol($object->repeat_type, $repeat_type));
		
		if ($have_permission)
		{
			$form->register("", null, new htmlcontrol("<h1>For Administrate Approval</h1>"));
			
			$capprove = array();
			$capprove[0]="Not Approve";
			$capprove[1]="Checked, but not approve";
			$capprove[2]="Approved";
			$form->register("approve","Approve", new dropdowncontrol($object->approve, $capprove));
		}
		
		$form->register('submit','',new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));

		return $form;
	}

	function update($values,$object, $loc) {
		global $user;
		
		$object->title = $values['title'];

		$object->body = _ab_safe_html_string($values['body']);

		$object->eventstart = yuidatetimecontrol::parseData('eventstart',$values);
		$object->eventend = yuidatetimecontrol::parseData('eventend',$values);
		$object->repeat_type = $values['repeat_type'];
		
		// for easy sql query when render calendar, we save the day and month of this event
		$object->eventstart_day=date("j", $object->eventstart);
		$object->eventstart_month=date("n", $object->eventstart);

		if (!isset($object->id)) {
			global $user;
			$object->poster = $user->id;
			$object->posted = aboid_getCurrentTime();
			$object->approve = 0;
		}
		
		 $have_permission=exponent_permissions_check(array("administrate", "approve", "manage_approval"), $loc);
		 if ($have_permission)
		 {
			 $object->edited=aboid_getCurrentTime();
			 $object->editor=$user->id;
			 $object->approve=intval($values['approve']);
			 $object->approve_date=aboid_getCurrentTime();
			 global $user;
			 $object->approver="{$user->firstname} {$user->lastname}({$user->username})";
		 }
		return $object;
	}
}

?>
